package com.fingard.dsp.bank.directbank.gdb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class GDB01Balance extends GDB01Base implements IBalance {

    /**
     * 单个余额查询，统一采用流处理方式
     * 1、循环调用Req7506BalQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
     * 2、发银行
     * 3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
     * 然后调用RetAts7506BalQry.writeRetDetail()，写入返回ATS的明细
     */
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            //赋值通用参数
            GDB01Header bankHeader = new GDB01Header();
            bankHeader.tranCode = "0001";
            bankHeader.retCode = "000";//固定值，比填项
            bankHeader.entSeqNo = p_req7506.reqHeader.transSeqID;
            while (p_req7506.readRecord()) {//不支持批量查询，逐个查询
                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("Body");
                //写入账号
                xmlWriter.writeElementString("account", p_req7506.readedDetail.actNum);
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //System.out.println("xmlWriter.toXmlString():" + xmlWriter.toXmlString());
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                //如果返回tmpStrRet[0]表示返回connMsg，connMsg 为空则表示响应正常，
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        //获取银行响应代码，返回值为000或002表示处理成功，否则为处理失败
                        String tmpRetCode = tmpBkReader.getFirstText("/BEDC/Message/commHead/retCode");
                        if (tmpRetCode != null && (tmpRetCode.equals("000") || tmpRetCode.equals("002"))) {
                            if (tmpBkReader.read("/BEDC/Message/Body")) {
                                tmpRetDetail.respCode = "0";
                                //获取币种
                                tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                                //获取账户
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("account");
                                //获取可用余额，如果balSign为“+”表示金额为正，为“-”表示金额为负
                                tmpRetDetail.availBal = (tmpBkReader.readedXElement.getSingleText("balSign").equals("+")) ?
                                        (tmpBkReader.readedXElement.getSingleText("balance")) : ("-" + tmpBkReader.readedXElement.getSingleText("balance"));
                                //获取冻结金额
                                tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("freeze");
                                //获取实际余额
                                if(StringHelper.isNullOrEmpty(tmpRetDetail.availBal)){
                                    tmpRetDetail.availBal="0.00";
                                }
                                if(StringHelper.isNullOrEmpty(tmpRetDetail.frozenBal)){
                                    tmpRetDetail.frozenBal="0.00";
                                }
                                Double currBal = Double.valueOf(tmpRetDetail.availBal) + Double.valueOf(tmpRetDetail.frozenBal);
                                tmpRetDetail.currBal = new java.text.DecimalFormat("0.00").format(currBal);
                                //获取最后交易时间
                                tmpRetDetail.lastTranDate = tmpBkReader.readedXElement.getSingleText("lastDate");
                            }
                        }
                        //处理失败respCode返回为-1
                        else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo=translateRetCode(tmpRetCode);
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            GDB01Header bankHeader = new GDB01Header();
            bankHeader.tranCode = "0028";
            bankHeader.retCode = "000";//固定值，比填项
            bankHeader.entSeqNo = p_req7505.reqHeader.transSeqID;
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入ICBC02通用的头部节点
            writeCommonHeader(xmlWriter,bankHeader);
            xmlWriter.writeStartElement("Body");
            //写入账号
            xmlWriter.writeElementString("account", p_req7505.actNum);
            xmlWriter.writeElementString("beginDate", p_req7505.dateFrom);
            xmlWriter.writeElementString("endDate", p_req7505.dateTo);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
            if (tmpRetBkStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    //获取银行响应代码，返回值为000或002表示处理成功，否则为处理失败
                    String tmpRetCode = tmpBkReader.getFirstText("/BEDC/Message/commHead/retCode");
                    if (tmpRetCode != null && (tmpRetCode.equals("000") || tmpRetCode.equals("002"))) {
                        while (tmpBkReader.read("/BEDC/Message/Body/records/record")) {
                            RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                            String tmpETYDAT = tmpBkReader.readedXElement.getSingleText("queryDate");
                            if (tmpETYDAT.length() == 8) {
                                tmpRetDetail.balDate = tmpETYDAT.substring(0, 4) + "-" + tmpETYDAT.substring(4, 6) + "-" + tmpETYDAT.substring(6, 8);
                            } else {
                                tmpRetDetail.balDate = tmpETYDAT;
                            }
                            tmpRetDetail.actBal = tmpBkReader.readedXElement.getSingleText("avaiBalance");
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        //tmpRespInfo = tmpRetMsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpRetBkStr[1];
            }

        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }

}
